<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">

      <el-button
        type="primary"
        @click="handleAdd">添加</el-button>
      <!--列表-->
      <el-table :data="ConsumableList" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column type="index" width="60"> </el-table-column>
        <el-table-column prop="name" label="名称" sortable> </el-table-column>
        <el-table-column prop="unit" label="单位" sortable> </el-table-column>
        <el-table-column prop="originalStock" label="总量" sortable> </el-table-column>
        <el-table-column prop="usage" label="已分配量" sortable> </el-table-column>
        <el-table-column prop="usage_count" label="已使用量" sortable> </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="small"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleAllot(scope.$index, scope.row)">分配</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
        </el-pagination>
      </el-col>


      <el-dialog title="添加耗材" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="editable">
          <el-form-item label="设备名称">
            <el-input v-model="editable.name"> </el-input>
          </el-form-item>
          <el-form-item label="单位">
            <el-input v-model="editable.unit"> </el-input>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="editable.originalStock"> </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="createConsumable">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="分配耗材" :visible.sync="dialogFormVisible1" width="30%">
        <el-form :model="allotable">
          <el-form-item label="工位">
            <el-select v-model="allotable.stationList" multiple placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="allotable.perUsage" label-width="20px"> </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="handleAllotConsum">确 定</el-button>
        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>
  import headTop from './head.vue'
  import { getConsumableList, deleteConsumable, createConsumable, stationListC, allotConsumable, postStationConsumableAll } from '@/api/api'
  export default {
    data () {
      return {
        loading: false,
        options: [],
        ConsumableList: [],
        total: 0,
        page: 1,
        editable: {
          name: '',
          unit: '',
          originalStock: 0
        },
        allotable: {
          gid: 1,
          rowid: 0,
          stationList: [],
          perUsage: 0
        },
        dialogFormVisible: false,
        dialogFormVisible1: false,
        menuOptions: [],
        selectMenu: {},
        selectIndex: null
      }
    },
    components: {
      headTop
    },
    created () {
      this.initData()
    },
    methods: {
      // 获取设备名称
      async initData () {
        this.loading = true
        this.editable = {
          name: '',
          unit: '',
          originalStock: 0
        } // 重置输入框内容
        try {
          getConsumableList(this.page).then((res) => {
            this.total = res.data.count
            this.ConsumableList = res.data.results  // type 看下转换下，通过store获取
            this.loading = false
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      async getStationList () {
        this.options = []
        try {
          stationListC().then((res) => {
            res.data.results.forEach((item, index) => {
              this.options.push({
                label: item.serialNum,
                value: item.id,
                index
              })
            })
          })
        } catch (err) {
          console.log('获取设备名称失败', err)
        }
      },
      handleCurrentChange (val) {
        this.page = val
        this.initData()
      },
      handleAdd () {
        this.dialogFormVisible = true
      },
      handleAllot (index, row) {
        this.allotable = {
          stationList: [],
          perUsage: 0,
          rowid: row.id,
          gid: 1
        }
        this.getStationList()
        this.dialogFormVisible1 = true
      },
      handleSelect (index) {
        this.selectIndex = index
        this.selectMenu = this.menuOptions[index]
      },
      async createConsumable () {
        this.dialogFormVisible = false
        let postData = {
          name: this.editable.name,
          unit: this.editable.unit,
          originalStock: this.editable.originalStock
        }
        createConsumable(postData).then(res => {
          if (res.status === 201) {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.initData()
          } else {
            this.$message({
              type: 'error',
              message: '添加失败'
            })
          }
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '添加失败'
          })
          console.log('类型添加失败', error)
        })
      },
      handleDelete (index, row) {
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.loading = true
          deleteConsumable(row.id).then(res => {
            this.initData()
            this.loading = false
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          }).catch(error => {
            this.$message({
              type: 'error',
              message: '删除失败'
            })
            console.log('类型删除失败', error)
          })
        }).catch(() => {
        })
      },
      async handleAllotConsum () {
        let postData = {
          cid: this.allotable.rowid,
          gid: this.allotable.gid,
          stationList: this.allotable.stationList,
          perUsage: this.allotable.perUsage
        }
        allotConsumable(postData).then(res => {
          this.dialogFormVisible1 = false
          this.initData()
          this.$message({
            type: 'success',
            message: '分配成功'
          })
        }, res => {
          this.$message({
            type: 'error',
            message: '分配失败'
          })
        })
      },
      handleEdit (index, row) {
        this.$prompt('总量大小：', '修改总量', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /\d+/,
          inputErrorMessage: '请输入数字'
        }).then(({ value }) => {
          let data = {cid: row.id, gid: 1, originalStock: value}
          postStationConsumableAll(data).then(res => {
            this.initData()
            this.$message({
              type: 'success',
              message: res.data
            })
          }, res => {
            this.$message({
              type: 'error',
              message: res.response.data
            })
          })
        }).catch(() => {
        })
      }
    }
  }

</script>

<style scoped>
  .el-button {
    margin: 5px;
  }
</style>
